<?php

if(config::read('users_subdomains'))
{
   # CHECK FOR DOMAIN OWNERS
   if ($_SERVER['HTTP_HOST'] != $_SYS['domain'] && $_SERVER['HTTP_HOST'] != 'www.'. $_SYS['domain'])
   {
      $subdomain_user = str_replace($_SYS['domain'], '', $_SERVER['HTTP_HOST']);
      $subdomain_user = str_replace('.', '', $subdomain_user);

      if (strpos($subdomain_user, 'www') === 0)
      {
         $subdomain_user = substr($subdomain_user, 3);
      }
      $subdomain_user = $USERS->getUser($subdomain_user);

      if (!empty($subdomain_user) && in_array('PERSONAL', $subdomain_user->roles))
      {
         $SMARTY->assign('subdomain_user', $subdomain_user);
      } else
      {
         header('location: http://' . $_SYS['domain']);
      }
   } else
   {
      $subdomain_user = $USERS->getUser(2);
      $SMARTY->assign('subdomain_user', $subdomain_user);
   }
}


# PLUGINS HOOK
$startups_to_include = array();
# INCLUDE LOCAL INDEX STARTUP
$requestPath = $REQUEST->getRequestPath();

if (empty($requestPath) && file_exists(ROOT . '/engine/index.inc'))
{
   $startups_to_include[] = ROOT . '/engine/index.inc';
}
# INCLUDE GLOBAL INDEX STARTUP
if (file_exists(ROOT . '/engine/index.global.inc'))
{
   $startups_to_include[] = ROOT . '/engine/index.global.inc';
}



$tpl_type = 'base';  # Default Template TYPE

if (!empty($requestPath))
{
   $i = -1;
   foreach($requestPath AS $chank)
   {
      $error404 = TRUE;
      $i++;

      if ($FILTER->isValidInteger($chank))
      {
         # If node is integer then it should be an article
         $article = new article($chank, $category->id); // $ARTICLE->getArticle($request[$i], $category['id']);
         if (!empty($article) && !empty($article->id))
         {
            $tpl_type = 'article';
            $error404 = FALSE;
         }

         # Include startup for previous node if it was last
         if (!empty($category) && file_exists(ROOT . '/engine/' . $category->alias . '.inc'))
         {
            $startups_to_include[] = ROOT . '/engine/' . $category->alias . '.inc';
            $error404 = FALSE;
         }
         break;
      } else
      {
         # Work with tree nodes
         $node_name = $chank;
         $parent_node = !empty($category) ? $category->id : false;
         $node = $TREE->getNode($node_name, $parent_node);
         if (!empty($node))
         {
            $category = $node;

            # Include startup for last node
            if ($i == $REQUEST->getRequestLength() && file_exists(ROOT . '/engine/' . $category->alias . '.inc'))
            {
               $startups_to_include[] = ROOT . '/engine/' . $category->alias . '.inc';
            }

            # Include global startup for node
            if (file_exists(ROOT . '/engine/' . $node_name . '.global.inc'))
            {
               $startups_to_include[] = ROOT . '/engine/' . $node_name . '.global.inc';
            }

            $error404 = FALSE;
         } elseif (!empty($category) && file_exists(ROOT . '/engine/' . $category->alias . '.inc'))
         {
            # Include startup for previous node if it was last
            $startups_to_include[] = ROOT . '/engine/' . $category->alias . '.inc';
            $error404 = FALSE;

            break;
         } else
         {
            break;
         }
      }
   }
}
unset($node);


if (!empty($error404))
{
   $category = $TREE->getNode(404);
} elseif (empty($category))
{
   $category = $TREE->getNode(500);
}
$SMARTY->setCategory($category);



# INCLUDE STARTUPS (PLUGINS)
if(!empty($startups_to_include)) foreach($startups_to_include AS $include_file)
{
   require_once($include_file);
}


// GET ARTICLES FOR CATEGORY
if (empty($article) && empty($articles))
{
   paginator::setItemsOnPage(($category->i_onpage ? $category->i_onpage : 100));

   $ARTICLES = new articles();
   $ARTICLES->setOrder('position');

   $set['cat'] = array('type' => 'exact', 'value' => $category->id);
   $set['active'] = array('type' => 'exact', 'value' => 1);
   
   # SET TYPE FILTER
   if(request::getRequest('ContentTypes'))
   {
      $set['types'] = array('type' => 'have', 'value' => request::getRequest('ContentTypes'));
   }
   
   # SET FIELDS FILTER
   if(request::getRequest('filters'))
   {
      foreach(request::getRequest('filters') AS $f=>$v)
      {
         $set[$f] = array('type' => 'exact', 'value' => $v);
      }
   }
   

   $articles = $ARTICLES->getArticles(array('field' => $set));

   if(count($articles) == 1 && !paginator::getPages())
   {
      # JUST ONE ARTICLE
      $tpl_type = 'article';
      $article = array_shift($articles);
      $articles = false;
   } elseif (count($articles) > 1 || paginator::getPages())
   {
      $tpl_type = 'articles';
   } else
   {
      $articles = false;
   }
   unset($set);
}

if(empty($tpl_type))
{
   $tpl_type = 'article';
}

$SMARTY->setTemplates($category->id, $tpl_type);



# GET META DATA
if ($article && $tpl_type == 'article')
{
   $meta['title'] = $article->metatitle        ? htmlspecialchars(strip_tags($article->metatitle)) : htmlspecialchars(strip_tags($article->title));
   $meta['words'] = $article->metawords        ? htmlspecialchars(strip_tags($article->metawords)) : ($category->metawords ? htmlspecialchars(strip_tags($category->metawords))   : gettext::translate('site', 'metawords'));
   $meta['description'] = $article->metadescr  ? htmlspecialchars(strip_tags($article->metadescr)) : ($article->description ? htmlspecialchars(strip_tags($article->description)) : ($category->metadescr ? htmlspecialchars(strip_tags($category->metadescr)) : gettext::translate('site', 'metadescr')));
} else
{
   $meta['title'] = $category->metatitle       ? htmlspecialchars(strip_tags($category->metatitle)) : htmlspecialchars(strip_tags($category->title));
   $meta['words'] = $category->metawords       ? htmlspecialchars(strip_tags($category->metawords)) : gettext::translate('site', 'metawords');
   $meta['description'] = $category->metadescr ? htmlspecialchars(strip_tags($category->metadescr)) : ($category->description ? htmlspecialchars(strip_tags($category->description)) : gettext::translate('site', 'metadescr'));
}

